import { Component, OnInit, Input, Output, EventEmitter, SimpleChanges } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入

@Component({
  selector: 'sa-common-item-modal',
  templateUrl: './common-items-modal.html'
})
export class CommonItemsModalComponent implements OnInit {
  @Input() componentCode: any //固件key
  @Input() companyIds: any //之前选中的公司
  @Input() isShowModal: any = false //显示弹窗
  @Output() closeDialog = new EventEmitter()
  companyIdListValue: any = '' // 选中公司
  companyArrays: any = []; // 公司多选数组
  companyParentList: any = [] //公司树全部数据
  inputApi: string = 'company/getParentCompany.html'
  constructor(private jsonApiService: JsonApiService) { }
  ngOnInit() { }

  ngOnChanges(changes: SimpleChanges): void {
    if (this.isShowModal) {
      this.companyIdListValue = this.companyIds
      if (this.companyIds) {
        if (this.companyParentList.length === 0) {
          this.getParentCompany()
        } else {
          this.echoCompanyData()
        }
      }
      $('#' + this.componentCode + 'Div').show()
    }
  }

  // 在组件销毁之前调用
  ngOnDestroy() { }

  closeCommonItems(refresh) {
    this.isShowModal = false
    $('#' + this.componentCode + 'Div').hide()
    let emitObj = {
      isShowModal: this.isShowModal, //关闭弹窗
      isRefresh: refresh,
      companyIds: this.companyIdListValue
    }
    this.closeDialog.emit(emitObj)
    this.companyIdListValue = ''
    this.companyArrays = []
  }
  submitCommonItems() {
    if (this.companyArrays.length > 10) {
      this.jsonApiService.message('最多只能选择10个项目')
      return
    }
    let postParam = new FormData()
    postParam.append('companyIds', this.companyIdListValue)
    postParam.append('componentCode', this.componentCode) //固件key名称
    this.jsonApiService.form_http('control/station/config/addComponentConfig.html', postParam).subscribe(data => {
      this.jsonApiService.message(data.message)
      if (data.success) {
        this.closeCommonItems(true)
      }
    })
  }
  // 公司多选返回
  addCompanySelected(data) {
    this.companyArrays = data.companySelectedList
    if (this.companyArrays.length > 0) {
      // console.log(this.companyArrays, 'this.companyArrays')
      this.companyIdListValue = this.companyArrays.map(item => { return item.companyId })
      this.companyIdListValue = this.companyIdListValue.join(',')
    } else {
      this.companyIdListValue = ''
    }
  }
  //获取所有父级公司的数据
  getParentCompany() {
    this.jsonApiService.http_get('company/getParentCompany.html').subscribe(data => {
      this.companyParentList = data
      this.echoCompanyData()
    })
  }
  //回显选中项目数据
  echoCompanyData() {
    let objList = []
    if (this.companyParentList.length > 0) {
      let list = this.companyIds.split(',')
      list.forEach(el => {
        this.companyParentList.forEach(item => {
          if (el === item.id) {
            let obj = {
              companyId: el,
              companyName: item.text
            }
            objList.push(obj)
          }
        })
      })
    }
    this.companyArrays = JSON.parse(JSON.stringify(objList))
    // console.log(this.companyArrays, 'this.companyArrays')
  }
}